package com.yxinsur.imc.web.controller.business;

import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yxinsur.imc.business.domain.ImcIns;
import com.yxinsur.imc.business.enums.IsDeletedEnum;
import com.yxinsur.imc.business.form.RepoInsForm;
import com.yxinsur.imc.business.service.IImcInsCatalogService;
import com.yxinsur.imc.business.service.IImcInsService;
import com.yxinsur.imc.business.vo.RepoInsVo;
import com.yxinsur.imc.business.vo.select.SelectIns;
import com.yxinsur.imc.common.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yxinsur.imc.common.annotation.Log;
import com.yxinsur.imc.common.core.controller.BaseController;
import com.yxinsur.imc.common.core.domain.AjaxResult;
import com.yxinsur.imc.common.enums.BusinessType;
import com.yxinsur.imc.business.domain.ImcRepoIns;
import com.yxinsur.imc.business.service.IImcRepoInsService;
import com.yxinsur.imc.common.utils.poi.ExcelUtil;
import com.yxinsur.imc.common.core.page.TableDataInfo;

/**
 * 保险产品库(险种)Controller
 * 
 * @author imc
 * @date 2021-03-26
 */
@Api(value="保险产品库接口服务",tags={"4.1.保险产品库接口服务"})
@RestController
@RequestMapping("/business/repo/ins")
public class ImcRepoInsController extends BaseController
{
    @Autowired
    private IImcRepoInsService imcRepoInsService;
    @Autowired
    private IImcInsService imcInsService;
    @Autowired
    private IImcInsCatalogService imcInsCatalogService;

    /**
     * 查询保险产品库(险种)列表
     */
    @ApiOperation("保险产品库分页列表查询")
    @GetMapping("/list")
    public TableDataInfo list(RepoInsForm repoInsForm)
    {
        startPage();
        List<RepoInsVo> list = imcRepoInsService.selectImcRepoInsList(repoInsForm);
        return getDataTable(list);
    }

    /**
     * 导出保险产品库(险种)列表
     */
    @ApiOperation("保险产品库导出数据查询")
    @Log(title = "保险产品库(险种)", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(RepoInsForm repoInsForm)
    {
        List<RepoInsVo> list = imcRepoInsService.selectImcRepoInsList(repoInsForm);
        ExcelUtil<RepoInsVo> util = new ExcelUtil<RepoInsVo>(RepoInsVo.class);
        return util.exportExcel(list, "产品库列表数据");
    }

    /**
     * 获取保险产品库(险种)详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success();
    }

    /**
     * 新增保险产品库(险种)
     */
    @ApiOperation("保险产品库新增")
    @Log(title = "保险产品库(险种)", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RepoInsForm repoInsForm)
    {
        return toAjax(imcRepoInsService.insertImcRepoIns(repoInsForm));
    }

    /**
     * 修改保险产品库(险种)
     */
    @ApiOperation("保险产品库更新")
    @Log(title = "保险产品库(险种)", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RepoInsForm repoInsForm)
    {
        return toAjax(imcRepoInsService.updateImcRepoIns(repoInsForm));
    }

    /**
     * 删除保险产品库(险种)
     */
    @ApiOperation("保险产品库批量删除")
    @Log(title = "保险产品库(险种)", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(imcRepoInsService.deleteImcRepoInsByIds(ids));
    }

    /**
     * 获取非我的产品数据
     */
    @ApiOperation("获取非我的产品数据")
    @PostMapping("/get/{repoSupplierId}/notmyins")
    public List<SelectIns> addDataList(@PathVariable Long repoSupplierId)
    {
        QueryWrapper<ImcIns> insWrapper = new QueryWrapper<>();
        insWrapper.eq("is_deleted", IsDeletedEnum.N.getCode());
        insWrapper.eq("tenant_id", SecurityUtils.getTenantId());
        List<ImcIns> supplierList = this.imcInsService.list(insWrapper);
        List<Long> insIds = new ArrayList<>();
        insIds.add(-1L);
        supplierList.stream().forEach(s ->{
            insIds.add(s.getRepoInsId());
        });
        //查询权限配置的产品列表
        QueryWrapper<ImcRepoIns> repoInsWrapper = new QueryWrapper<>();
        repoInsWrapper.notIn("id",insIds);
        repoInsWrapper.eq("repo_supplier_id",repoSupplierId);
        repoInsWrapper.eq("is_deleted",IsDeletedEnum.N.getCode());
        //repoSupplierWrapper.eq("tenant_id",SecurityUtils.getTenantId());
        List<ImcRepoIns> repoInsList = this.imcRepoInsService.list(repoInsWrapper);
        List<SelectIns> selectList = new ArrayList<>();
        repoInsList.stream().forEach(i ->{
            SelectIns selectIns = new SelectIns();
            BeanUtils.copyProperties(i,selectIns);
            selectList.add(selectIns);
        });
        return selectList;
    }
    /**
     * 获取险种分类树数据
     */
    @ApiOperation("获取险种分类树数据")
    @PostMapping("/catalog/data")
    public AjaxResult catalogData()
    {
        return AjaxResult.success(this.imcInsCatalogService.getTreeSelectList());
    }
}
